const db = wx.cloud.database();
const Travel = db.collection('cnov');

let search = (where, page, limit, cb) => {
  let _where = {}

  if (where.date){
    _where.t_date = where.date;
  } 

  if (where.no) {
    _where.t_no = db.RegExp({
      regexp: where.no,
      options: 'i',
    })
  } 

  console.log(_where)

  Travel.where(_where)
    .skip(page * limit)
    .limit(limit)
    .orderBy('t_date', 'desc')
    .get()
    .then(res => {
      cb(res)
    })
    .catch(console.error)
}

let total = (cb) => {
  Travel.count()
    .then(res => {
      cb(res)
    })
    .catch(console.error)
}

let get = (id, cb) => {
  Travel.doc(id)
    .get()
    .then(res => {
      cb(res)
    })
    .catch(console.error)
}

module.exports = {
  search,
  get,
  total
}
